<?php

class MY_Upload extends CI_Upload {
    var $my_upload_path = '';
    public function __construct($config = array()){
        parent::__construct($config);
        $this->my_upload_path = $config['upload_path'];
    }
    public function do_upload($field = 'userfile'){
        return parent::do_upload($field);
//         if ($re = parent::do_upload($field)) {
//             //这里判断是否已开启上传阿里云OSS的配置(需要在nginx或apache中设置ENV变量), 再上传到阿里云
// //             if (isset($_SERVER["USE_ALIOSS_UPLOAD"])&&$_SERVER["USE_ALIOSS_UPLOAD"]==="true") {
//                 /*--------------------对路径做统一处理----------------------------------------*/
//                 $replace_str = array('./', '../', '//');
//                 foreach ($replace_str as $v) {
//                     $this->my_upload_path = str_replace($v, '/', $this->my_upload_path);
//                 }
//                 if (substr($this->my_upload_path, 0, 1) === '/') {
//                     $this->my_upload_path = substr($this->my_upload_path, 1);
//                 }
//                 if (substr($this->my_upload_path, -1) !== '/') {
//                     $this->my_upload_path .= '/';
//                 }
//                 /*------------------------路径处理结束----------------------------------------*/

//                 $file_name = $this->my_upload_path . $this->file_name; //拼接服务器文件路径
//                 $this->_CI->load->library('cls_oss');
//                 $oss = new Cls_oss();
//                 $oss->bucket = MB_ALIOSS_BUCKET;//相当于项目名，根路径
//                 $xiexian_pos = strrpos($file_name, '/');
//                 $dir = substr($file_name, 0, $xiexian_pos);
//                 $oss->create_directory($dir);//尝试创建目录
//                 $object = $dir . substr($file_name, $xiexian_pos, strlen($file_name) - $xiexian_pos);//上传到oss服务器的路径和文件名

//                 try {
//                     //oss上传文件处理
//                     $RS = $oss->upload_by_file($object, $file_name);
//                     if (isset($RS['obj_url']) && !empty($RS['obj_url'])) {//返回文件url证明上传成功
//                         if ($delete_local) {//如果需要删除服务器上的原文件
//                             //上传完成后删除原文件
//                             delete_files($file_name);
//                         }
//                         $this->file_url = $RS['obj_url'];
//                     } else {//否则将错误信息准备好
//                         $this->set_error('upload failed,upload rs:' . json_encode($RS));
//                     }
//                 } catch (Exception $ex) {//上传过程中的错误捕获
//                     $this->set_error(is_array($ex->getMessage()) ? json_encode($ex->getMessage()) : $ex->getMessage());
//                 }
//             }
// //         }
//         return $re;
    }
    
    //图片剪切完重新上传OSS
    public function cropUpload()
    {
        //这里判断是否已开启上传阿里云OSS的配置(需要在nginx或apache中设置ENV变量), 再上传到阿里云
//         if (isset($_SERVER["USE_ALIOSS_UPLOAD"])&&$_SERVER["USE_ALIOSS_UPLOAD"]==="true") {
            /*--------------------对路径做统一处理----------------------------------------*/
            $replace_str = array('./', '../', '//');
            foreach ($replace_str as $v) {
                $this->my_upload_path = str_replace($v, '/', $this->my_upload_path);
            }
            if (substr($this->my_upload_path, 0, 1) === '/') {
                $this->my_upload_path = substr($this->my_upload_path, 1);
            }
            if (substr($this->my_upload_path, -1) !== '/') {
                $this->my_upload_path .= '/';
            }
            /*------------------------路径处理结束----------------------------------------*/
        
            $file_name = $this->my_upload_path . $this->file_name; //拼接服务器文件路径
            $this->_CI->load->library('cls_oss');
            $oss = new Cls_oss();
            $oss->bucket = MB_ALIOSS_BUCKET;//相当于项目名，根路径
            $xiexian_pos = strrpos($file_name, '/');
            $dir = substr($file_name, 0, $xiexian_pos);
            $oss->create_directory($dir);//尝试创建目录
            $object = $dir . substr($file_name, $xiexian_pos, strlen($file_name) - $xiexian_pos);//上传到oss服务器的路径和文件名
        
            try {
                //oss上传文件处理
                $RS = $oss->upload_by_file($object, $file_name);
                if (isset($RS['obj_url']) && !empty($RS['obj_url'])) {//返回文件url证明上传成功
                    $this->file_url = $RS['obj_url'];
                    return true;
                } else {//否则将错误信息准备好
                    $this->set_error('upload failed,upload rs:' . json_encode($RS));
                }
            } catch (Exception $ex) {//上传过程中的错误捕获
                $this->set_error(is_array($ex->getMessage()) ? json_encode($ex->getMessage()) : $ex->getMessage());
            }
//         }
    }
}
